package com.yxh.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yxh.base.PageEntity;
import com.yxh.dao.MenuMapper;
import com.yxh.dao.UnitMapper;
import com.yxh.exception.MenuException;
import com.yxh.exception.UnitException;
import com.yxh.pojo.Menu;
import com.yxh.service.MenuService;
import com.yxh.util.SnowflakeIdWorker;
import com.yxh.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.yxh.util.APIErrorCode.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * Created by 32988 on 2018/6/1.
 */
@Service("menuService")
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public PageInfo getInformation(Menu menu, Integer limit, Integer offset) {
        PageHelper.startPage(limit, offset);
        List<Menu> menuList = menuMapper.selectByMenu(menu);
        PageInfo pageInfo = new PageInfo(menuList);
        pageInfo.setList(menuList);
        return pageInfo;
    }

    @Override
    public int save(Menu record) {
        record.setMenuId(SnowflakeIdWorker.creatId());
        int count = menuMapper.insert(record);
        if (count == 0) {
            throw new MenuException(INTERNAL_ERROR);
        }
        return count;
    }

    @Override
    public int update(Menu record) {
        int count = menuMapper.updateByPrimaryKeySelective(record);
        return count;
    }

    @Override
    public int delete(String id) {

        if(!StringUtil.isEmpty(id)){
            String [] arr = id.split(",");
            for(String a:arr){
                Menu m=new Menu();
                m.setParentId(a);
                List<Menu> list= menuMapper.selectByMenu(m);
                if(list.size()!=0) throw new MenuException(NOT_DELETE_ERROR);
            }

            int count = menuMapper.deleteByIds(arr);
            if (count == 0) {
                throw new MenuException(PARAMS_INVALID);
            }
        }
        return 0;
    }

    @Override
    public void batchDelete(List<Menu> records) {

    }

    @Override
    public Menu get(String id) {
        return null;
    }

    @Override
    public Menu get(Menu record) {
        return null;
    }

    @Override
    public List<Menu> getList(Menu record) {
        return null;
    }

    @Override
    public List<Menu> getAllList(Menu record) {
        return null;
    }

    @Override
    public List<Menu> getListPage(PageEntity<Menu> record) {
        return null;
    }
}
